package com.soushin.mvpdemo.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hjq.toast.ToastUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * fragment基类
 *
 * @auther SouShin
 * @time 2018/12/26 17:27
 */
public abstract class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends SupportFragment {
    private P presenter;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        presenter = createPresenter();
        bindView();
    }

    protected abstract P createPresenter();

    protected abstract int getLayoutId();

    //这个方法是为了让用户不要忘了绑定BaseView
    protected abstract void bindView();

    protected P getPresenter() {
        return presenter;
    }

    public FragmentActivity getFragActivity() {
        return _mActivity;
    }

    public void goTo(Class clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    public void goTo(Class clazz, Intent intent) {
        intent.setClass(getActivity(), clazz);
        startActivity(intent);
    }

    public void showToasty(String msg) {
        ToastUtils.show(msg);
    }

    public void showProgress(String msg) {

    }

    public void disProgress() {

    }

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        if (presenter != null) {
            presenter.unsubscribe();
        }
        super.onDestroyView();
    }
}
